/*==============================================================*/
/* DBMS name:      Microsoft SQL Server 2008                    */
/* Created on:     22.06.14 12:52:40                            */
/*==============================================================*/


if exists (select 1
            from  sysindexes
           where  id    = object_id('ANALITIKA_MAGACINSKE_KARTICE')
            and   name  = 'STAVKA_DOKUMENTA_KOJA_JE_KREIRALA_STAVKU_ANALITIKE_MAG__KARTICE_FK'
            and   indid > 0
            and   indid < 255)
   drop index ANALITIKA_MAGACINSKE_KARTICE.STAVKA_DOKUMENTA_KOJA_JE_KREIRALA_STAVKU_ANALITIKE_MAG__KARTICE_FK
go

if exists (select 1
            from  sysobjects
           where  id = object_id('ANALITIKA_MAGACINSKE_KARTICE')
            and   type = 'U')
   drop table ANALITIKA_MAGACINSKE_KARTICE
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('ARTIKAL')
            and   name  = 'RELATIONSHIP_6_FK'
            and   indid > 0
            and   indid < 255)
   drop index ARTIKAL.RELATIONSHIP_6_FK
go

if exists (select 1
            from  sysobjects
           where  id = object_id('ARTIKAL')
            and   type = 'U')
   drop table ARTIKAL
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('CLAN_POPISNE_KOMISIJE')
            and   name  = 'RELATIONSHIP_22_FK'
            and   indid > 0
            and   indid < 255)
   drop index CLAN_POPISNE_KOMISIJE.RELATIONSHIP_22_FK
go

if exists (select 1
            from  sysobjects
           where  id = object_id('CLAN_POPISNE_KOMISIJE')
            and   type = 'U')
   drop table CLAN_POPISNE_KOMISIJE
go

if exists (select 1
            from  sysobjects
           where  id = object_id('GRUPA_ARTIKALA')
            and   type = 'U')
   drop table GRUPA_ARTIKALA
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('MAGACIN')
            and   name  = 'RELATIONSHIP_8_FK'
            and   indid > 0
            and   indid < 255)
   drop index MAGACIN.RELATIONSHIP_8_FK
go

if exists (select 1
            from  sysobjects
           where  id = object_id('MAGACIN')
            and   type = 'U')
   drop table MAGACIN
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('MAGACINSKA_KARTICA')
            and   name  = 'RELATIONSHIP_4_FK'
            and   indid > 0
            and   indid < 255)
   drop index MAGACINSKA_KARTICA.RELATIONSHIP_4_FK
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('MAGACINSKA_KARTICA')
            and   name  = 'RELATIONSHIP_3_FK'
            and   indid > 0
            and   indid < 255)
   drop index MAGACINSKA_KARTICA.RELATIONSHIP_3_FK
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('MAGACINSKA_KARTICA')
            and   name  = 'RELATIONSHIP_2_FK'
            and   indid > 0
            and   indid < 255)
   drop index MAGACINSKA_KARTICA.RELATIONSHIP_2_FK
go

if exists (select 1
            from  sysobjects
           where  id = object_id('MAGACINSKA_KARTICA')
            and   type = 'U')
   drop table MAGACINSKA_KARTICA
go

if exists (select 1
            from  sysobjects
           where  id = object_id('POPISNA_KOMISIJA')
            and   type = 'U')
   drop table POPISNA_KOMISIJA
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('POPISNI_DOKUMENT')
            and   name  = 'RELATIONSHIP_18_FK'
            and   indid > 0
            and   indid < 255)
   drop index POPISNI_DOKUMENT.RELATIONSHIP_18_FK
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('POPISNI_DOKUMENT')
            and   name  = 'RELATIONSHIP_17_FK'
            and   indid > 0
            and   indid < 255)
   drop index POPISNI_DOKUMENT.RELATIONSHIP_17_FK
go

if exists (select 1
            from  sysobjects
           where  id = object_id('POPISNI_DOKUMENT')
            and   type = 'U')
   drop table POPISNI_DOKUMENT
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('POSLOVNA_GODINA')
            and   name  = 'RELATIONSHIP_5_FK'
            and   indid > 0
            and   indid < 255)
   drop index POSLOVNA_GODINA.RELATIONSHIP_5_FK
go

if exists (select 1
            from  sysobjects
           where  id = object_id('POSLOVNA_GODINA')
            and   type = 'U')
   drop table POSLOVNA_GODINA
go

if exists (select 1
            from  sysobjects
           where  id = object_id('POSLOVNI_PARTNER')
            and   type = 'U')
   drop table POSLOVNI_PARTNER
go

if exists (select 1
            from  sysobjects
           where  id = object_id('PREDUZECE')
            and   type = 'U')
   drop table PREDUZECE
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('PROMETNI_DOKUMENT')
            and   name  = 'RELATIONSHIP_14_FK'
            and   indid > 0
            and   indid < 255)
   drop index PROMETNI_DOKUMENT.RELATIONSHIP_14_FK
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('PROMETNI_DOKUMENT')
            and   name  = 'KUPAC_ILI_DOBAVLJAC_FK'
            and   indid > 0
            and   indid < 255)
   drop index PROMETNI_DOKUMENT.KUPAC_ILI_DOBAVLJAC_FK
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('PROMETNI_DOKUMENT')
            and   name  = 'ZA_MEDJUMAGACINSKI_TRANSFER___ULAZNI_FK'
            and   indid > 0
            and   indid < 255)
   drop index PROMETNI_DOKUMENT.ZA_MEDJUMAGACINSKI_TRANSFER___ULAZNI_FK
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('PROMETNI_DOKUMENT')
            and   name  = 'MAGACIN_U_PROMETU_FK'
            and   indid > 0
            and   indid < 255)
   drop index PROMETNI_DOKUMENT.MAGACIN_U_PROMETU_FK
go

if exists (select 1
            from  sysobjects
           where  id = object_id('PROMETNI_DOKUMENT')
            and   type = 'U')
   drop table PROMETNI_DOKUMENT
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('RADNIK')
            and   name  = 'ZAPOSLEN_FK'
            and   indid > 0
            and   indid < 255)
   drop index RADNIK.ZAPOSLEN_FK
go

if exists (select 1
            from  sysobjects
           where  id = object_id('RADNIK')
            and   type = 'U')
   drop table RADNIK
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('SEKTOR')
            and   name  = 'RELATIONSHIP_7_FK'
            and   indid > 0
            and   indid < 255)
   drop index SEKTOR.RELATIONSHIP_7_FK
go

if exists (select 1
            from  sysobjects
           where  id = object_id('SEKTOR')
            and   type = 'U')
   drop table SEKTOR
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('STAVKA_PROMETNOG_DOKUMENTA')
            and   name  = 'RELATIONSHIP_12_FK'
            and   indid > 0
            and   indid < 255)
   drop index STAVKA_PROMETNOG_DOKUMENTA.RELATIONSHIP_12_FK
go

if exists (select 1
            from  sysobjects
           where  id = object_id('STAVKA_PROMETNOG_DOKUMENTA')
            and   type = 'U')
   drop table STAVKA_PROMETNOG_DOKUMENTA
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('STAVKE_POPISA')
            and   name  = 'RELATIONSHIP_20_FK'
            and   indid > 0
            and   indid < 255)
   drop index STAVKE_POPISA.RELATIONSHIP_20_FK
go

if exists (select 1
            from  sysobjects
           where  id = object_id('STAVKE_POPISA')
            and   type = 'U')
   drop table STAVKE_POPISA
go

if exists(select 1 from systypes where name='CENA')
   execute sp_unbindrule CENA
go

if exists(select 1 from systypes where name='CENA')
   drop type CENA
go

if exists(select 1 from systypes where name='KOLICINA')
   execute sp_unbindrule KOLICINA
go

if exists(select 1 from systypes where name='KOLICINA')
   drop type KOLICINA
go

if exists(select 1 from systypes where name='REDNI_BROJ')
   execute sp_unbindrule REDNI_BROJ
go

if exists(select 1 from systypes where name='REDNI_BROJ')
   drop type REDNI_BROJ
go

if exists(select 1 from systypes where name='VRSTA_DOKUMENTA')
   execute sp_unbindrule VRSTA_DOKUMENTA
go

if exists(select 1 from systypes where name='VRSTA_DOKUMENTA')
   drop type VRSTA_DOKUMENTA
go

if exists (select 1
   from  sysobjects where type = 'D'
   and name = 'D_0'
   )
   drop default D_0
go

if exists (select 1
   from  sysobjects where type = 'D'
   and name = 'D_1'
   )
   drop default D_1
go

if exists (select 1 from sysobjects where id=object_id('R_CENA') and type='R')
   drop rule  R_CENA
go

if exists (select 1 from sysobjects where id=object_id('R_KOLICINA') and type='R')
   drop rule  R_KOLICINA
go

if exists (select 1 from sysobjects where id=object_id('R_REDNI_BROJ') and type='R')
   drop rule  R_REDNI_BROJ
go

if exists (select 1 from sysobjects where id=object_id('R_VRSTA_DOKUMENTA') and type='R')
   drop rule  R_VRSTA_DOKUMENTA
go

create rule R_CENA as
      @column >= 0
go

create rule R_KOLICINA as
      @column >= 0
go

create rule R_REDNI_BROJ as
      @column >= 1
go

create rule R_VRSTA_DOKUMENTA as
      @column in ('P','O','M')
go

/*==============================================================*/
/* Default: D_0                                                 */
/*==============================================================*/
create default D_0
    as 0
go

/*==============================================================*/
/* Default: D_1                                                 */
/*==============================================================*/
create default D_1
    as 1
go

/*==============================================================*/
/* Domain: CENA                                                 */
/*==============================================================*/
create type CENA
   from money
go

execute sp_bindrule R_CENA, CENA
go

execute sp_bindefault D_0, 'CENA'
go

/*==============================================================*/
/* Domain: KOLICINA                                             */
/*==============================================================*/
create type KOLICINA
   from numeric(12,2)
go

execute sp_bindrule R_KOLICINA, KOLICINA
go

execute sp_bindefault D_0, 'KOLICINA'
go

/*==============================================================*/
/* Domain: REDNI_BROJ                                           */
/*==============================================================*/
create type REDNI_BROJ
   from numeric(10)
go

execute sp_bindrule R_REDNI_BROJ, REDNI_BROJ
go

execute sp_bindefault D_1, 'REDNI_BROJ'
go

/*==============================================================*/
/* Domain: VRSTA_DOKUMENTA                                      */
/*==============================================================*/
create type VRSTA_DOKUMENTA
   from char(1)
go

execute sp_bindrule R_VRSTA_DOKUMENTA, VRSTA_DOKUMENTA
go

/*==============================================================*/
/* Table: ANALITIKA_MAGACINSKE_KARTICE                          */
/*==============================================================*/
create table ANALITIKA_MAGACINSKE_KARTICE (
   SIFRA_PREDUZECA      varchar(12)          not null,
   SIFRA_SEKTORA        varchar(12)          not null,
   SIFRA_MAGACINA       varchar(12)          not null,
   SIFRA_GRUPE          varchar(12)          not null,
   SIFRA_ARTIKLA        numeric(12)          not null,
   POSLOVNA_GODINA      numeric(4)           not null
      constraint CKC_POSLOVNA_GODINA_ANALITIK check (POSLOVNA_GODINA >= 0),
   REDNI_BROJ_AN_MAG_KART REDNI_BROJ           not null,
   BROJ_PROMETNOG_DOK   REDNI_BROJ           null,
   DATUM_NASTANKA_PROM_DOK datetime             null,
   REDNI_BROJ_ST_PROM_DOK REDNI_BROJ           null,
   DATUM_PROMENE        datetime             null,
   VRSTA_DOKUMENTA      VRSTA_DOKUMENTA      null,
   SIFRA_DOKUMENTA      varchar(12)          null,
   KOLICINA             numeric(12,2)        null default 0,
   CENA                 CENA                 null,
   VREDNOST             money                null default 0,
   SMER                 char(1)              null
      constraint CKC_SMER_ANALITIK check (SMER is null or (SMER in ('U','I'))),
   constraint PK_ANALITIKA_MAGACINSKE_KARTIC primary key (SIFRA_PREDUZECA, SIFRA_SEKTORA, SIFRA_MAGACINA, SIFRA_GRUPE, SIFRA_ARTIKLA, POSLOVNA_GODINA, REDNI_BROJ_AN_MAG_KART)
)
go

/*===========================================================================*/
/* Index: STAVKA_DOKUMENTA_KOJA_JE_KREIRALA_STAVKU_ANALITIKE_MAG__KARTICE_FK */
/*===========================================================================*/
create index STAVKA_DOKUMENTA_KOJA_JE_KREIRALA_STAVKU_ANALITIKE_MAG__KARTICE_FK on ANALITIKA_MAGACINSKE_KARTICE (
POSLOVNA_GODINA ASC,
BROJ_PROMETNOG_DOK ASC,
DATUM_NASTANKA_PROM_DOK ASC,
REDNI_BROJ_ST_PROM_DOK ASC,
SIFRA_PREDUZECA ASC
)
go

/*==============================================================*/
/* Table: ARTIKAL                                               */
/*==============================================================*/
create table ARTIKAL (
   SIFRA_GRUPE          varchar(12)          not null,
   SIFRA_ARTIKLA        numeric(12)          not null,
   PAKOVANJE            numeric(7,2)         not null
      constraint CKC_PAKOVANJE_ARTIKAL check (PAKOVANJE >= 0),
   JEDINICA_MERE        varchar(5)           not null,
   NAZIV_ARTIKLA        varchar(30)          not null,
   constraint PK_ARTIKAL primary key nonclustered (SIFRA_GRUPE, SIFRA_ARTIKLA)
)
go

/*==============================================================*/
/* Index: RELATIONSHIP_6_FK                                     */
/*==============================================================*/
create index RELATIONSHIP_6_FK on ARTIKAL (
SIFRA_GRUPE ASC
)
go

/*==============================================================*/
/* Table: CLAN_POPISNE_KOMISIJE                                 */
/*==============================================================*/
create table CLAN_POPISNE_KOMISIJE (
   ID_KOMISIJE          numeric(7)           not null,
   JMBG                 numeric(13)          not null,
   SIFRA_PREDUZECA      varchar(12)          not null,
   IME                  varchar(30)          not null,
   PREZIME              varchar(30)          not null,
   KORISNICKO_IME       varchar(30)          not null,
   LOZINKA              varchar(30)          not null,
   constraint PK_CLAN_POPISNE_KOMISIJE primary key (ID_KOMISIJE, JMBG)
)
go

/*==============================================================*/
/* Index: RELATIONSHIP_22_FK                                    */
/*==============================================================*/
create index RELATIONSHIP_22_FK on CLAN_POPISNE_KOMISIJE (
ID_KOMISIJE ASC
)
go

/*==============================================================*/
/* Table: GRUPA_ARTIKALA                                        */
/*==============================================================*/
create table GRUPA_ARTIKALA (
   SIFRA_GRUPE          varchar(12)          not null,
   NAZIV_GRUPE          varchar(30)          not null,
   constraint PK_GRUPA_ARTIKALA primary key nonclustered (SIFRA_GRUPE)
)
go

/*==============================================================*/
/* Table: MAGACIN                                               */
/*==============================================================*/
create table MAGACIN (
   SIFRA_PREDUZECA      varchar(12)          not null,
   SIFRA_SEKTORA        varchar(12)          not null,
   SIFRA_MAGACINA       varchar(12)          not null,
   NAZIV_MAGACINA       varchar(30)          not null,
   constraint PK_MAGACIN primary key nonclustered (SIFRA_PREDUZECA, SIFRA_SEKTORA, SIFRA_MAGACINA)
)
go

/*==============================================================*/
/* Index: RELATIONSHIP_8_FK                                     */
/*==============================================================*/
create index RELATIONSHIP_8_FK on MAGACIN (
SIFRA_PREDUZECA ASC,
SIFRA_SEKTORA ASC
)
go

/*==============================================================*/
/* Table: MAGACINSKA_KARTICA                                    */
/*==============================================================*/
create table MAGACINSKA_KARTICA (
   SIFRA_PREDUZECA      varchar(12)          not null,
   SIFRA_SEKTORA        varchar(12)          not null,
   SIFRA_MAGACINA       varchar(12)          not null,
   SIFRA_GRUPE          varchar(12)          not null,
   SIFRA_ARTIKLA        numeric(12)          not null,
   POSLOVNA_GODINA      numeric(4)           not null
      constraint CKC_POSLOVNA_GODINA_MAGACINS check (POSLOVNA_GODINA >= 0),
   PROSECNA_CENA        CENA                 not null,
   ZADNJA_NABAVNA_CENA  CENA                 null,
   PLANSKA_CENA         CENA                 null,
   KOL_POCETNA          KOLICINA             not null,
   KOL_ULAZA            KOLICINA             null,
   KOL_IZLAZA           KOLICINA             null,
   KOL_UKUPNA           KOLICINA             null,
   VR_POCETNA           CENA                 null,
   VR_ULAZA             CENA                 null,
   VR_IZLAZA            CENA                 null,
   VR_UKUPNA            CENA                 null,
   constraint PK_MAGACINSKA_KARTICA primary key (SIFRA_PREDUZECA, SIFRA_SEKTORA, SIFRA_MAGACINA, SIFRA_GRUPE, SIFRA_ARTIKLA, POSLOVNA_GODINA)
)
go

/*==============================================================*/
/* Index: RELATIONSHIP_2_FK                                     */
/*==============================================================*/
create index RELATIONSHIP_2_FK on MAGACINSKA_KARTICA (
SIFRA_PREDUZECA ASC,
SIFRA_SEKTORA ASC,
SIFRA_MAGACINA ASC
)
go

/*==============================================================*/
/* Index: RELATIONSHIP_3_FK                                     */
/*==============================================================*/
create index RELATIONSHIP_3_FK on MAGACINSKA_KARTICA (
SIFRA_GRUPE ASC,
SIFRA_ARTIKLA ASC
)
go

/*==============================================================*/
/* Index: RELATIONSHIP_4_FK                                     */
/*==============================================================*/
create index RELATIONSHIP_4_FK on MAGACINSKA_KARTICA (
SIFRA_PREDUZECA ASC,
POSLOVNA_GODINA ASC
)
go

/*==============================================================*/
/* Table: POPISNA_KOMISIJA                                      */
/*==============================================================*/
create table POPISNA_KOMISIJA (
   ID_KOMISIJE          numeric(7)           not null,
   DATUM_FORMIRANJA_KOMISIJE datetime             not null default current_timestamp,
   constraint PK_POPISNA_KOMISIJA primary key (ID_KOMISIJE)
)
go

/*==============================================================*/
/* Table: POPISNI_DOKUMENT                                      */
/*==============================================================*/
create table POPISNI_DOKUMENT (
   ID_KOMISIJE          numeric(7)           not null,
   POSLOVNA_GODINA      numeric(4)           not null
      constraint CKC_POSLOVNA_GODINA_POPISNI_ check (POSLOVNA_GODINA >= 0),
   SIFRA_PREDUZECA      varchar(12)          not null,
   SIFRA_SEKTORA        varchar(12)          not null,
   SIFRA_MAGACINA       varchar(12)          not null,
   REDNI_BROJ_POPISA    REDNI_BROJ           not null,
   DATUM_POPISA         datetime             not null default getdate(),
   STATUS               char(1)              null default 'F'
      constraint CKC_STATUS_POPISNI_ check (STATUS is null or (STATUS in ('F','P','S'))),
   constraint PK_POPISNI_DOKUMENT primary key nonclustered (SIFRA_PREDUZECA, SIFRA_SEKTORA, ID_KOMISIJE, POSLOVNA_GODINA, SIFRA_MAGACINA, REDNI_BROJ_POPISA, DATUM_POPISA)
)
go

/*==============================================================*/
/* Index: RELATIONSHIP_17_FK                                    */
/*==============================================================*/
create index RELATIONSHIP_17_FK on POPISNI_DOKUMENT (
SIFRA_PREDUZECA ASC,
POSLOVNA_GODINA ASC
)
go

/*==============================================================*/
/* Index: RELATIONSHIP_18_FK                                    */
/*==============================================================*/
create index RELATIONSHIP_18_FK on POPISNI_DOKUMENT (
SIFRA_PREDUZECA ASC,
SIFRA_SEKTORA ASC,
SIFRA_MAGACINA ASC
)
go

/*==============================================================*/
/* Table: POSLOVNA_GODINA                                       */
/*==============================================================*/
create table POSLOVNA_GODINA (
   SIFRA_PREDUZECA      varchar(12)          not null,
   POSLOVNA_GODINA      numeric(4)           not null
      constraint CKC_POSLOVNA_GODINA_POSLOVNA check (POSLOVNA_GODINA >= 0),
   DATUM_POCETKA_POSLOVNE_GODINE datetime             not null default getdate(),
   DATUM_KRAJA_POSLOVNE_GODINE datetime             not null,
   constraint PK_POSLOVNA_GODINA primary key nonclustered (SIFRA_PREDUZECA, POSLOVNA_GODINA)
)
go

/*==============================================================*/
/* Index: RELATIONSHIP_5_FK                                     */
/*==============================================================*/
create index RELATIONSHIP_5_FK on POSLOVNA_GODINA (
SIFRA_PREDUZECA ASC
)
go

/*==============================================================*/
/* Table: POSLOVNI_PARTNER                                      */
/*==============================================================*/
create table POSLOVNI_PARTNER (
   SIFRA_POS_PARTN      varchar(12)          not null,
   NAZIV_POS_PARTN      varchar(30)          not null,
   constraint PK_POSLOVNI_PARTNER primary key nonclustered (SIFRA_POS_PARTN)
)
go

/*==============================================================*/
/* Table: PREDUZECE                                             */
/*==============================================================*/
create table PREDUZECE (
   SIFRA_PREDUZECA      varchar(12)          not null,
   NAZIV_PREDUZECA      varchar(30)          not null,
   constraint PK_PREDUZECE primary key nonclustered (SIFRA_PREDUZECA)
)
go

/*==============================================================*/
/* Table: PROMETNI_DOKUMENT                                     */
/*==============================================================*/
create table PROMETNI_DOKUMENT (
   POSLOVNA_GODINA      numeric(4)           not null
      constraint CKC_POSLOVNA_GODINA_PROMETNI check (POSLOVNA_GODINA >= 0),
   SIFRA_PREDUZECA      varchar(12)          not null,
   BROJ_PROMETNOG_DOK   REDNI_BROJ           not null,
   DATUM_NASTANKA_PROM_DOK datetime             not null default getdate(),
   SIFRA_SEKTORA        varchar(12)          not null,
   SIFRA_MAGACINA       varchar(12)          not null,
   SIFRA_POS_PARTN      varchar(12)          null,
   MAG_SIFRA_PREDUZECA  varchar(12)          null,
   MAG_SIFRA_SEKTORA    varchar(12)          null,
   MAG_SIFRA_MAGACINA   varchar(12)          null,
   DATUM_KNJIZENJA      datetime             null,
   VRSTA_DOKUMENTA      VRSTA_DOKUMENTA      not null,
   STATUS               char(1)              not null default 'F'
      constraint CKC_STATUS_PROMETNI check (STATUS in ('F','P','S')),
   constraint PK_PROMETNI_DOKUMENT primary key nonclustered (POSLOVNA_GODINA, SIFRA_PREDUZECA, BROJ_PROMETNOG_DOK, DATUM_NASTANKA_PROM_DOK)
)
go

/*==============================================================*/
/* Index: MAGACIN_U_PROMETU_FK                                  */
/*==============================================================*/
create index MAGACIN_U_PROMETU_FK on PROMETNI_DOKUMENT (
SIFRA_PREDUZECA ASC,
SIFRA_SEKTORA ASC,
SIFRA_MAGACINA ASC
)
go

/*==============================================================*/
/* Index: ZA_MEDJUMAGACINSKI_TRANSFER___ULAZNI_FK               */
/*==============================================================*/
create index ZA_MEDJUMAGACINSKI_TRANSFER___ULAZNI_FK on PROMETNI_DOKUMENT (
MAG_SIFRA_PREDUZECA ASC,
MAG_SIFRA_SEKTORA ASC,
MAG_SIFRA_MAGACINA ASC
)
go

/*==============================================================*/
/* Index: KUPAC_ILI_DOBAVLJAC_FK                                */
/*==============================================================*/
create index KUPAC_ILI_DOBAVLJAC_FK on PROMETNI_DOKUMENT (
SIFRA_POS_PARTN ASC
)
go

/*==============================================================*/
/* Index: RELATIONSHIP_14_FK                                    */
/*==============================================================*/
create index RELATIONSHIP_14_FK on PROMETNI_DOKUMENT (
SIFRA_PREDUZECA ASC,
POSLOVNA_GODINA ASC
)
go

/*==============================================================*/
/* Table: RADNIK                                                */
/*==============================================================*/
create table RADNIK (
   JMBG                 numeric(13)          not null,
   SIFRA_PREDUZECA      varchar(12)          not null,
   IME                  varchar(30)          not null,
   PREZIME              varchar(30)          not null,
   KORISNICKO_IME       varchar(30)          not null,
   LOZINKA              varchar(30)          not null,
   constraint PK_RADNIK primary key nonclustered (JMBG)
)
go

/*==============================================================*/
/* Index: ZAPOSLEN_FK                                           */
/*==============================================================*/
create index ZAPOSLEN_FK on RADNIK (
SIFRA_PREDUZECA ASC
)
go

/*==============================================================*/
/* Table: SEKTOR                                                */
/*==============================================================*/
create table SEKTOR (
   SIFRA_PREDUZECA      varchar(12)          not null,
   SIFRA_SEKTORA        varchar(12)          not null,
   NAZIV_SEKTORA        varchar(30)          not null,
   constraint PK_SEKTOR primary key nonclustered (SIFRA_PREDUZECA, SIFRA_SEKTORA)
)
go

/*==============================================================*/
/* Index: RELATIONSHIP_7_FK                                     */
/*==============================================================*/
create index RELATIONSHIP_7_FK on SEKTOR (
SIFRA_PREDUZECA ASC
)
go

/*==============================================================*/
/* Table: STAVKA_PROMETNOG_DOKUMENTA                            */
/*==============================================================*/
create table STAVKA_PROMETNOG_DOKUMENTA (
   SIFRA_PREDUZECA      varchar(12)          not null,
   POSLOVNA_GODINA      numeric(4)           not null
      constraint CKC_POSLOVNA_GODINA_STAVKA_P check (POSLOVNA_GODINA >= 0),
   BROJ_PROMETNOG_DOK   REDNI_BROJ           not null,
   DATUM_NASTANKA_PROM_DOK datetime             not null default getdate(),
   REDNI_BROJ_ST_PROM_DOK REDNI_BROJ           not null,
   SIFRA_GRUPE          varchar(12)          not null,
   SIFRA_ARTIKLA        numeric(12)          not null,
   KOLICINA             numeric(12,2)        not null default 0,
   CENA                 CENA                 not null,
   VREDNOST             money                not null default 0,
   constraint PK_STAVKA_PROMETNOG_DOKUMENTA primary key (POSLOVNA_GODINA, BROJ_PROMETNOG_DOK, DATUM_NASTANKA_PROM_DOK, REDNI_BROJ_ST_PROM_DOK, SIFRA_PREDUZECA)
)
go

/*==============================================================*/
/* Index: RELATIONSHIP_12_FK                                    */
/*==============================================================*/
create index RELATIONSHIP_12_FK on STAVKA_PROMETNOG_DOKUMENTA (
SIFRA_GRUPE ASC,
SIFRA_ARTIKLA ASC
)
go

/*==============================================================*/
/* Table: STAVKE_POPISA                                         */
/*==============================================================*/
create table STAVKE_POPISA (
   SIFRA_PREDUZECA      varchar(12)          not null,
   SIFRA_SEKTORA        varchar(12)          not null,
   ID_KOMISIJE          numeric(7)           not null,
   POSLOVNA_GODINA      numeric(4)           not null
      constraint CKC_POSLOVNA_GODINA_STAVKE_P check (POSLOVNA_GODINA >= 0),
   SIFRA_MAGACINA       varchar(12)          not null,
   REDNI_BROJ_POPISA    REDNI_BROJ           not null,
   DATUM_POPISA         datetime             not null default getdate(),
   REDNI_BROJ_ST_POPISA REDNI_BROJ           not null,
   SIFRA_GRUPE          varchar(12)          not null,
   SIFRA_ARTIKLA        numeric(12)          not null,
   KOLICINA_PO_POPISU   KOLICINA             null,
   KOLICINA_U_KARTICI   KOLICINA             null,
   constraint PK_STAVKE_POPISA primary key (SIFRA_PREDUZECA, SIFRA_SEKTORA, ID_KOMISIJE, POSLOVNA_GODINA, SIFRA_MAGACINA, REDNI_BROJ_POPISA, DATUM_POPISA, REDNI_BROJ_ST_POPISA)
)
go

/*==============================================================*/
/* Index: RELATIONSHIP_20_FK                                    */
/*==============================================================*/
create index RELATIONSHIP_20_FK on STAVKE_POPISA (
SIFRA_GRUPE ASC,
SIFRA_ARTIKLA ASC
)
go

